package com.example.jchuah.tiles;

import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class FlingDetector extends GestureDetector.SimpleOnGestureListener {

    protected FlingListener receiver;

    public FlingDetector(FlingListener receiver) {
        this.receiver = receiver;
    }

    View touched = null;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        if (Math.abs(velocityX) > 3 || Math.abs(velocityY) > 3) {
            // yes, a fling has occurred

            if (Math.abs(velocityX) > Math.abs(velocityY)) {
                // a horizontal fling occurred

                if (velocityX > 0) {
                    // fling to the right

                    Animation anim = AnimationUtils.loadAnimation(touched.getContext(), R.anim.flingright);
                    anim.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            touched.post(new Runnable() {
                                @Override
                                public void run() {

                                    receiver.onFlingRight(touched);
                                    touched.post(new Runnable() {

                                        @Override
                                        public void run() {
                                            touched.clearAnimation();
                                        }
                                    });
                                }
                            });
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
                    touched.startAnimation(anim);

                } else {
                    // fling to the left
                    Animation anim = AnimationUtils.loadAnimation(touched.getContext(), R.anim.flingleft);
                    anim.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            touched.post(new Runnable() {
                                @Override
                                public void run() {

                                    receiver.onFlingLeft(touched);
                                    touched.post(new Runnable() {

                                        @Override
                                        public void run() {
                                            touched.clearAnimation();
                                        }
                                    });
                                }
                            });

                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
                    touched.startAnimation(anim);

                }

            } else {
                // a vertical fling stuff
                if (velocityY > 0) {
                    // fling down
                    Animation anim = AnimationUtils.loadAnimation(touched.getContext(), R.anim.flingdown);
                    anim.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            touched.post(new Runnable() {
                                @Override
                                public void run() {

                                    receiver.onFlingDown(touched);
                                    touched.post(new Runnable() {

                                        @Override
                                        public void run() {
                                            touched.clearAnimation();
                                        }
                                    });

                                }
                            });
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
                    touched.startAnimation(anim);


                } else {
                    // fling up
                    Animation anim = AnimationUtils.loadAnimation(touched.getContext(), R.anim.flingup);
                    anim.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            touched.post(new Runnable() {
                                @Override
                                public void run() {
                                    receiver.onFlingUp(touched);
                                    touched.post(new Runnable() {

                                        @Override
                                        public void run() {
                                            touched.clearAnimation();
                                        }
                                    });

                                }
                            });
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
                    touched.startAnimation(anim);

                }
            }
        }

        return super.onFling(e1, e2, velocityX, velocityY);
    }
    public interface FlingListener {
        public void onFlingUp(View view);
        public void onFlingDown(View view);
        public void onFlingLeft(View view);
        public void onFlingRight(View view);
    }
}